﻿using BarcodeDelay.bean;
using BarcodeDelay.business;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BarcodeDelay.view
{
    public partial class FrmMachine : Form
    {
        public FrmMachine()
        {
            InitializeComponent();
        }
        ArrayList arListStaNames = new ArrayList();
        

        private void FrmMachine_Load(object sender, EventArgs e)
        {
           
            try
            {

            }
            catch(Exception ex)
            {

            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string strContent = this.richTextBox1.Text;
                if(strContent.Length<50)
                {
                    MessageBox.Show("授权不正常");
                    return;
                }
                string wechat = this.txtWechat.Text.Trim();

                try
                {
                    JObject jsonObjTmp = (JObject)JsonConvert.DeserializeObject(strContent);
                }
                catch(Exception ext)
                {
                    if(!strContent.Trim().EndsWith("}"))
                    {
                        if(strContent.Contains("sysResourceTree"))
                        {
                            strContent = strContent.Substring(0, strContent.IndexOf("sysResourceTree") - 2);
                            strContent = strContent + "}}";
                        }
                    }
                }
                JObject jsonObj = (JObject)JsonConvert.DeserializeObject(strContent);
                JObject dta = (JObject)jsonObj["data"];
                JToken token = (JToken)dta["token"];
                JToken loginTime= (JToken)dta["loginTime"];
                JToken networkName = (JToken)dta["networkName"];
                JToken name = (JToken)dta["name"];
                JToken networkCode= (JToken)dta["networkCode"];
                JToken networkId = (JToken)dta["networkId"];
                
                if (token==null||token.ToString().Length<32)
                {
                    MessageBox.Show("授权不正常");
                    return;
                }

                if(networkName.ToString().Contains("南通崇川网点"))
                {
                    if(name.ToString()!= "宗洁")
                    {
                        MessageBox.Show("网点未授权");
                        return;
                    }
                }
                if (!MemberInfo.Instance.htStaMap.ContainsKey(networkName.ToString()))
                {
                    MessageBox.Show("网点未授权");
                    return;
                }
                if(name!=null)
                {
                    StoParams.Instance.register_user = name.ToString();
                }
                StoParams.Instance.register_site = networkName.ToString();
                StoParams.Instance.register_code = networkCode.ToString();
                StoParams.Instance.register_networkId = networkId.ToString();
                //临时测试
                //StoParams.Instance.register_site = "湖州吴兴织里网点";
                string strFilter = StoParams.Instance.register_site;
                if(MemberInfo.Instance.htFilterMap.ContainsKey(networkName.ToString()))
                {
                    strFilter= strFilter + "," + MemberInfo.Instance.htFilterMap[networkName.ToString()].ToString();
                }
                //DateTime dtExpireTime = Convert.ToDateTime(htStaMap[networkName.ToString()].ToString());
                DateTime dtExpire = Convert.ToDateTime(MemberInfo.Instance.htStaMap[networkName.ToString()].ToString()); ;
                DateTime dtLoginTime = Convert.ToDateTime(loginTime.ToString());
                DateTime dtNow = System.DateTime.Now;
                if(dtNow.Date!= dtLoginTime.Date)
                {
                    MessageBox.Show("已过期，不能使用");
                    return;
                }
                TimeSpan tsT = dtNow - dtLoginTime;
                if(Math.Abs(tsT.TotalHours)>1)
                {
                    MessageBox.Show("系统日期不合法");
                    return;
                }

                if(dtLoginTime> dtExpire)
                {
                    MessageBox.Show("已过期，不能使用");
                    return;
                }
                TimeSpan ts = dtExpire - dtLoginTime;
                if (ts.TotalDays<5)
                {
                    MessageBox.Show("请尽快续费，免得影响使用 有效剩余日期"+ ts.TotalDays.ToString("0.0"));
                }
                StoParams.Instance.htTokenSign[token.ToString()]= networkName.ToString();


                StoParams.Instance.htRegSiteNameAndReg[StoParams.Instance.register_site] = networkId.ToString();
                StoParams.Instance.htRegSiteNameAndToken[StoParams.Instance.register_site] = token.ToString();

                recordSite(StoParams.Instance.register_site + " " + StoParams.Instance.register_code + " " + StoParams.Instance.register_user);
                MemberInfo.Instance.token = token.ToString();
                StoParams.Instance.htRegsiteToken[StoParams.Instance.register_user] = token.ToString();

                RegBean regBean = new RegBean();
                regBean.token = token.ToString();
                regBean.regSiteName = StoParams.Instance.register_site;
                regBean.regSiteID = networkId.ToString();
                regBean.regSiteCode = StoParams.Instance.register_code;
                regBean.regWechate = wechat;
                regBean.isAnthorized = true;
                StoParams.Instance.register_wechat= wechat;
                if(!StoParams.Instance.htRegInfo.ContainsKey(regBean.regSiteName))
                {
                    ArrayList arListRegInfo = new ArrayList();
                    arListRegInfo.Add(regBean);
                    StoParams.Instance.htRegInfo[regBean.regSiteName] = arListRegInfo;
                }
                else
                {
                    ArrayList arListRegInfo = StoParams.Instance.htRegInfo[regBean.regSiteName] as ArrayList;
                    arListRegInfo.Add(regBean);
                }
                //StoParams.Instance.htRegInfo[regBean.regSiteName] = regBean;
                MemberInfo.Instance.filter = strFilter;
                UtilityHelp.Instance.recordSiteWechat(StoParams.Instance.register_site, wechat);
                StoParams.Instance.htWechatInfo[StoParams.Instance.register_site] = wechat;
                StoParams.Instance.isRegByMachine = true;
                this.Close();

            }
            catch(Exception ex)
            {
                MessageBox.Show("授权不正常");
            }
        }

        private void recordSite(string siteName)
        {
            try
            {
                string delFile = System.Environment.CurrentDirectory + "\\" + "site.txt";
                FileStream bFile = new FileStream(delFile, FileMode.Create);
                StreamWriter swb = null;
                if (swb == null)
                {
                    swb = new StreamWriter(bFile);
                }
                swb.WriteLine(siteName);
                swb.Close();

            }
            catch (Exception ex)
            {

            }
        }

        

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {
            try
            {
                string strContent = this.richTextBox1.Text;
     
                JObject jsonObj = (JObject)JsonConvert.DeserializeObject(strContent);
                JObject dta = (JObject)jsonObj["data"];
                JToken token = (JToken)dta["token"];
                JToken loginTime = (JToken)dta["loginTime"];
                JToken networkName = (JToken)dta["networkName"];
                JToken name = (JToken)dta["name"];
                JToken networkCode = (JToken)dta["networkCode"];
                JToken networkId = (JToken)dta["networkId"];
                StoParams.Instance.register_site = networkName.ToString();
                if(StoParams.Instance.htWechatInfo.ContainsKey(StoParams.Instance.register_site))
                {
                    string wechat = StoParams.Instance.htWechatInfo[StoParams.Instance.register_site].ToString();
                    txtWechat.Text = wechat;
                }
            }
            catch(Exception ex)
            {

            }
        }
    }
}
